<template>
	<view class="app-public-supper-app-login" :style="[AppSkinVar]">
		<view class="e-p-t-60 e-m-b-60 e-flex-x-center">
			<image class="app-public-supper-app-login__logo" :src="`/static/${tenant}.png`" ></image>
		</view>
		<!-- <view class="e-p-l-72 e-p-r-72" v-if="hasInvitation">
			<view class="item e-flex  e-m-b-40" >
				<image class="img-36 e-m-r-30" src="/pages/public/static/public/invitation.png"></image>
				<e-input  v-model="salt" border="none" placeholder="推广人手机号" />
			</view>
		</view> -->
		<button class="app-public-supper-app-login__btn e-m-b-40 e-reset-button bg-primary c-white" @tap="handleLogin">手机号一键登录</button>
		<button class="app-public-supper-app-login__btn app-public-supper-app-login__btn-no e-reset-button" @tap="$goTo('/pages/public/account/appLogin')">账号密码登录</button>

		<view class="e-p-t-50 e-p-l-80 e-p-r-80 e-flex" @tap="isAgree = !isAgree" v-if="agreeShow || privacyShow">
			<radio style="transform: scale(0.8)" :checked="isAgree" :color="AppSkin.primary" @tap.stop="isAgree = !isAgree" />
			<text class="keep-all">{{ $t('public.我已阅读并同意') }}</text>
			<navigator class="c-primary keep-all" url="/pages/public/txt?id=13" v-if="agreeShow" @tap.stop>
				{{ $t('public.网络服务协议') }}
			</navigator>
			<template v-if="privacyShow">
				<text>{{ $t('public.与') }}</text>
				<navigator class="c-primary keep-all" url="/pages/public/txt?id=15" @tap.stop>{{ $t('public.隐私权政策') }}</navigator>
			</template>
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { postSupperAppLogin,postSupperAppBindRecommender } from '@/common/request/api';
import { useBasics } from '@/common/stores';
import { useCurrentInstance, useLogin } from '@/common/hooks';
import { storeToRefs } from 'pinia';
import { useInit } from '@/common/stores';
import { page } from '@/common/utils';

const ctx = useCurrentInstance();
const { login } = useLogin();
const { setTabbarPageIndex } = useInit();

const { agreement,hasInvitation } = storeToRefs(useBasics());
const agreeShow = computed(() => agreement.value.find((item) => item.id == 14)?.status === 1);
const privacyShow = computed(() => agreement.value.find((item) => item.id == 15)?.status === 1);

const logo = ref(uni.getStorageSync('logo'));
const tenant = uni.getStorageSync('tenant');
let openId = '';
const isAgree = ref(false);

const phone = ref('')

const handleLogin = () => {
	if (!isAgree.value && (privacyShow.value || agreeShow.value)) {
		ctx.$toastApp(ctx.$t('public.请先阅读并同意用户协议'));
		return;
	}
	uni.sendNativeEvent(
		'autoLogin',
		{
			msg: 'sendAutoLogin'
		},
		(ret:string) => {
			let params = {
				code: ret,
				salt: uni.getStorageSync('userSalt')
			};
			postSupperAppLogin(params)
				.then(async (res) => {
					uni.removeStorageSync('userSalt');
					const userInfo = await login(res.data.access_token);
					if(hasInvitation.value&&!userInfo.parentPhone){
						ctx.$goRedirectTo('/pages/public/account/bindRecommender')
						return 
					}
					
					const pagesPath = page(2);
					if (!pagesPath || pagesPath === '/' || pagesPath === 'pages/public/account/appLogin') {
						setTabbarPageIndex(0);
						ctx.$switchTab('/pages/tabbar/index');
					} else {
						ctx.$back();
					}
				})
				.catch((err) => {
					ctx.$toastApp(JSON.stringify(err));
					console.log(err);
				});
		}
	);
};
</script>
<style>
page {
	background: #fff;
}
</style>
<style lang="scss">
@include appBem_b(public-supper-app-login) {
	padding-top: 94px;
	@include appBem_e(logo) {
		width: 160rpx;
		height: 160rpx;
		border-radius: 20rpx;
	}
	@include appBem_e(btn) {
		width: 600rpx;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 36rpx;
		border-radius: 44rpx;
		margin: 0 auto;
	}
	@include appBem_e(btn-no) {
		border: 1px solid $base-color-text-secondary;
	}
	
	.item {
		background: #F7F7F7;
		border-radius: 50rpx;
		height: 90rpx;
		padding: 0 20rpx 0 40rpx;
	}
}
</style>
